package com.ctshk.sapp.iotc.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.iotc.dto.OperatePromotionListDTO;
import com.ctshk.rpc.iotc.dto.OperatePromotionQueryDTO;
import com.ctshk.rpc.iotc.req.*;
import com.ctshk.rpc.iotc.service.IOperatePromotionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 推广码 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-22
 */
@Api(value = "推广码", tags = "推广码")
@RestController
@RequestMapping("/operate/promotion")
public class OperatePromotionController extends BaseController {

    @DubboReference
    private IOperatePromotionService iOperatePromotionService;

    @ApiOperation(value = "新增推广码", notes = "新增推广码")
    @OperationLogAnnotation(operModul = OperationLogModel.IOTC, operType = OperationLogType.ADD, operTitle = "新增", operDesc = "新增推广码")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid OperatePromotionAddReq operatePromotionAddReq) {
        return iOperatePromotionService.add(operatePromotionAddReq, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "编辑推广码", notes = "编辑推广码")
    @OperationLogAnnotation(operModul = OperationLogModel.IOTC, operType = OperationLogType.UPDATE, operTitle = "编辑", operDesc = "编辑推广码")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid OperatePromotionUpdateReq req) {
        return iOperatePromotionService.update(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "修改推广码状态", notes = "修改推广码状态")
    @OperationLogAnnotation(operModul = OperationLogModel.IOTC, operType = OperationLogType.UPDATE)
    @PostMapping("/updateStatus")
    public Result update(@RequestBody @Valid CodeStatusReq req) {
        return iOperatePromotionService.updateStatus(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "提交审批", notes = "提交审批")
    @OperationLogAnnotation(operModul = OperationLogModel.IOTC, operType = OperationLogType.UPDATE)
    @GetMapping("/approvalCode/{id}")
    public Result approvalCode(@PathVariable Long id) {
        return iOperatePromotionService.approvalCode(id, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "查询推广码详情", notes = "查询推广码详情")
    @OperationLogAnnotation(operModul = OperationLogModel.IOTC, operType = OperationLogType.QUERY, operTitle = "查询", operDesc = "查询推广码详情")
    @PostMapping("/query")
    public Result<OperatePromotionQueryDTO> query(@RequestBody @Valid OperatePromotionQueryReq req) {
        return iOperatePromotionService.query(req);
    }

    @ApiOperation(value = "查询推广码列表", notes = "查询推广码列表")
    @OperationLogAnnotation(operModul = OperationLogModel.IOTC, operType = OperationLogType.QUERY, operTitle = "查询", operDesc = "查询推广码列表")
    @PostMapping("/list")
    public Result<PageResponse<OperatePromotionListDTO>> list(@RequestBody @Valid OperatePromotionListReq req) {
        return Result.success(iOperatePromotionService.list(req, getCurrentUser(getRequest())));
    }

    @ApiOperation(value = "查询并校验推广码详情", notes = "查询推广码详情")
    @OperationLogAnnotation(operModul = OperationLogModel.IOTC, operType = OperationLogType.QUERY, operTitle = "查询", operDesc = "查询并校验推广码详情")
    @PostMapping("/queryConform")
    public Result<OperatePromotionQueryDTO> queryConform(@RequestBody @Valid OperatePromotionConformReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return iOperatePromotionService.queryConform(req);
    }

    @ApiOperation(value = "(提供内部接口)扣减推广码剩余名额及剩余金额", notes = "扣减推广码剩余名额及剩余金额")
    @OperationLogAnnotation(operModul = OperationLogModel.IOTC, operType = OperationLogType.UPDATE, operTitle = "编辑", operDesc = "编辑推广码详情")
    @PostMapping("/deduct")
    public Result deduct(@RequestBody @Valid OperatePromotionDeductReq req) {
        return iOperatePromotionService.deduct(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "(提供内部接口)扣减推广码剩余名额及剩余金额退回", notes = "扣减推广码剩余名额及剩余金额退回")
    @OperationLogAnnotation(operModul = OperationLogModel.IOTC, operType = OperationLogType.UPDATE, operTitle = "编辑", operDesc = "编辑推广码详情")
    @PostMapping("/refund")
    public Result refund(@RequestBody @Valid OperatePromotionRefundReq req) {
        return iOperatePromotionService.refund(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "查询符合业务类型对应的产品ID集合", notes = "查询符合业务类型对应的产品ID集合")
    @OperationLogAnnotation(operModul = OperationLogModel.IOTC, operType = OperationLogType.QUERY, operTitle = "查询", operDesc = "查询银联优惠列表")
    @GetMapping("/queryRelationIdList/{businessType}")
    public Result<List<Long>> queryRelationIdList(@PathVariable Integer businessType) {
        return Result.success(iOperatePromotionService.queryRelationIdList(businessType));
    }

    @ApiOperation(value = "推广码审核", notes = "推广码审核")
    @OperationLogAnnotation(operModul = OperationLogModel.IOTC, operType = OperationLogType.UPDATE, operTitle = "推广码审核", operDesc = "推广码审核")
    @PostMapping("/approval")
    public Result approval(@RequestBody @Valid OperateBankApprovalReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return iOperatePromotionService.approval(req);
    }
}
